package com.panasonic.web.controller.cms;



import com.panasonic.web.domain.cms.CmsCategory;
import com.panasonic.web.model.TreeModel;
import com.panasonic.web.service.ICmsCategoryService;
import com.panasonic.web.vo.CmsBatchStatusVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.panasonic.common.annotation.Log;
import com.panasonic.common.core.controller.BaseController;
import com.panasonic.common.core.domain.AjaxResult;
import com.panasonic.common.enums.BusinessType;
/**
 * 类目Controller
 *
 * @author lwf
 * @date 2021-11-12
 */
@RestController
@RequestMapping("/cms/category")
@Api(value = "CmsCategory", tags = "类目")
public class CmsCategoryController extends BaseController {
    @Autowired
    private ICmsCategoryService cmsCategoryService;

    /**
     * 查询类目列表
     */
    @ApiOperation(value = "查询树结构")
    @GetMapping("/treeList")
    public AjaxResult treeList() {
        List<TreeModel> tree = cmsCategoryService.getCategoryTree();
        return AjaxResult.success(tree);
    }

    /**
     * 查询类目列表
     */
    @ApiOperation(value = "查询")
    @GetMapping("/list")
    public AjaxResult list(CmsCategory cmsCategory) {
        List<CmsCategory> list = cmsCategoryService.selectCmsCategoryList(cmsCategory);
        return AjaxResult.success(list);
    }

    /**
     * 获取类目详细信息
     */
    @ApiOperation(value = "获取类目详细信息")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return AjaxResult.success(cmsCategoryService.selectCmsCategoryById(categoryId));
    }

    /**
     * 新增类目
     */
    @ApiOperation(value = "新增类目")
    @Log(title = "类目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated CmsCategory cmsCategory) {
        return toAjax(cmsCategoryService.insertCmsCategory(cmsCategory));
    }

    /**
     * 修改类目
     */
    @ApiOperation(value = "修改类目")
    @Log(title = "类目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsCategory cmsCategory) {
        return toAjax(cmsCategoryService.updateCmsCategory(cmsCategory));
    }

    /**
     * 删除类目
     */
    @ApiOperation(value = "删除类目")
    @Log(title = "类目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(cmsCategoryService.deleteCmsCategoryByIds(categoryIds));
    }
    @ApiOperation(value = "修改状态，1 隐藏 0 显示")
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Validated CmsBatchStatusVo cmsBatchStatusVo) {
      if(cmsBatchStatusVo.getStatus().equals("0")|| cmsBatchStatusVo.getStatus().equals("1")){
          return toAjax(cmsCategoryService.changeStatus(cmsBatchStatusVo));
      }else{
          return AjaxResult.error("状态错误");
      }
    }

}
